package com.epfresh.pickerview.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.epfresh.supplier.R;

import java.util.List;

public class Validitypricedapter extends RecyclerView.Adapter<Validitypricedapter.BaseViewHolder> {
    /**
     *
     */
    private Context mContext;
    private List<String> mData;
    private OnItemClickListener mListener;
    private int selectorposition = -1;

    public Validitypricedapter(Context context, List<String> list) {
        this.mContext = context;
        this.mData = list;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_validity_price, parent, false);
        BaseViewHolder holder = new BaseViewHolder(view);
        return holder;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(BaseViewHolder holder, final int position) {
        if (position == selectorposition) {
            holder.itemView.setSelected(true);
            holder.iv_select.setVisibility(View.VISIBLE);
        } else {
            holder.itemView.setSelected(false);
            holder.iv_select.setVisibility(View.GONE);
        }
        holder.tv_validity.setText(mData.get(position)+"小时内有效");
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemClick(position);
                    notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mData != null && mData.size() != 0) {
            return mData.size();
        } else {
            return 0;
        }
    }

    class BaseViewHolder extends RecyclerView.ViewHolder {
        TextView tv_validity;
        ImageView iv_select;

        public BaseViewHolder(View itemView) {
            super(itemView);
            tv_validity = (TextView) itemView.findViewById(R.id.tv_validity);
            iv_select = (ImageView) itemView.findViewById(R.id.iv_select);

        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    public void setSelectedPosition(int selectedPosition) {
        selectorposition = selectedPosition;
    }

    public int getSelectedPosition() {
        return selectorposition;
    }

}

